/**
 * 搜索帮助
 * Created By zhangQ 2018/6/13
 */

import _ from 'lodash'

/**
 *
 * @param txt
 * @param sourceData
 * @param searchKeys
 * @param filterKey
 * @param filterVal
 */
export function searchArr (txt, sourceData, searchKeys, filterKey, filterVal) {
  let sourceDataClone = []
  // 类别筛选过滤
  if (filterKey && filterVal && filterVal.length > 0) {
    // let filterValarr = filterVal.split(',')
    sourceDataClone = _.filter(sourceData, function (sourceItem) {
      return _.some(filterVal, (fieldItem) => {
        return String(sourceItem[filterKey]) === String(fieldItem)
      })
    })
  } else {
    sourceDataClone = sourceData
  }
  if (!txt) {
    return sourceDataClone
  }
  let upTxt = txt.toUpperCase()
  let lowTxt = txt.toLowerCase()
  let fields = searchKeys.split(',')
  let resSource = _.filter(sourceDataClone, function (sourceItem) {
    return _.some(fields, (fieldItem) => {
      return (_.includes(sourceItem[fieldItem], upTxt) || _.includes(sourceItem[fieldItem], lowTxt))
    })
  })
  return resSource
}
